<?php
/**
 * 基本服务类
 *
 */
class CommonService extends Service {
	
	/**
	 */
	public function selectFootDoc(){
		$dictlist = service('Dict')->getAllDictByCode('doc_help');
		foreach ($dictlist as $vo){
			$sordCodes[] = $vo['code'];
		}
		$map['sort_id'] = array('in',$sordCodes);
		$map['is_active'] = 1;
		$map['is_on_footer'] = 1;
		//dump($map);
		$docs = M('document')->where($map)->field('document_id,title,sort_id')->order('`sort_id` ASC,`display_order` ASC,`document_id` ASC')->findAll();
		//dump($dictlist);
		//dump($docs);
		$data['dict'] = $dictlist;
		$data['docs'] = $docs;
		return $data;
	}
	
	/**
	 */
	public function selectBulletin($limit=0){

		$sordCodes[] = 'doc_bulletin';
		//$dictlist = service('Dict')->getAllDictByCode('doc_bulletin');
		$map['sort_id'] = array('in',$sordCodes);
		//dump($map);
		$tmp = M('document')->where($map)->field('document_id,title,sort_id,ctime')->order('`sort_id` ASC,`display_order` desc,`document_id` desc');
		if($limit==0)$docs = $tmp->findAll();
		else $docs = $tmp->limit($limit)->findAll();
		//dump($dictlist);
		//dump($docs);
		//$data['dict'] = $dictlist;
		$data = $docs;
		return $data;
	}
	
	public function getBulletinById($id){
		if(!empty($id))$map['document_id'] = $id;
		$map['sort_id'] = 'doc_bulletin';
		$doc = M('document')->where($map)->find();
		return $doc;
	}
	
	public function getDocById($id){
		if(!empty($id))$map['document_id'] = $id;
		$map['is_active'] = 1;
		$doc = M('document')->where($map)->find();
		return $doc;
	}
	
	public function getDocBySortCode($sortcode){
		$map['sort_id'] = $sortcode;
		$map['is_active'] = 1;
		$doc = M('document')->where($map)->find();
		return $doc;
	}
	
	public function getSubDocBySortCode($sortcode){
		$codes = service('Dict')->getAllDirectSubDicts($sortcode,'code');
		$map['sort_id'] = array('in',$codes);
		$map['is_active'] = 1;
		$docs = M('document')->where($map)->findAll();
		return $docs;
	}
	
	//提交错误报告
	private function submitError($ownerid,$ownertype,$question_text){
		return model('Dialogue')->addDialogue($ownerid,$ownertype,'error',$question_text);
	}
	
	//提交问题
	private function submitQuestion($ownerid,$ownertype,$question_text){
		return model('Dialogue')->addDialogue($ownerid,$ownertype,'question',$question_text);
	}
	
	//提交咨询
	private function submitSuggest($ownerid,$ownertype,$question_text){
		return model('Dialogue')->addDialogue($ownerid,$ownertype,'suggest',$question_text);
	}
	
	//添加游湖用户咨询，报错，问答
	public function submitDialogue($ownerid,$ownertype,$sort_id,$question_text){
		if(!in_array($sort_id,array('error','question','suggest'))){
			return 0;
		}
		$id = 0;
		if($sort_id=='suggest')$id = $this->submitSuggest($ownerid,$ownertype,$question_text);
		if($sort_id=='error')$id = $this->submitError($ownerid,$ownertype,$question_text);
		if($sort_id=='question')$id = $this->submitQuestion($ownerid,$ownertype,$question_text);
		return $id;
	}
	
	public function searchAdvisory4User($sort_id,$uid,$limit=20){
		$map['sort_id']=$sort_id;
		if(!empty($uid))$map['ownerid']=$uid;
		$map['ownertype']='user';
		$map['admin_id']=array('GT',0);
		return model('Dialogue')->searchDialogue($map,' id ',$limit);
	}
	
	public function searchAdvisory4SH(){
		
	}
	public function searchAdvisory4Sys($sort_id,$ownertype,$hadanswered,$ownerid,$limit=20){
		$map['sort_id']=$sort_id;
		if(!empty($ownerid))$map['ownerid']=$ownerid;
		
		if($ownertype=='user')$map['ownertype']=$ownertype;
		if($ownertype=='shanghu')$map['ownertype']=$ownertype;
		
		if($hadanswered=='Y')$map['admin_id']=array('GT',0);
		if($hadanswered=='N')$map['admin_id']=0;
		return model('Dialogue')->searchDialogue($map,' id ',$limit);
	}
	
	//服务初始化
	public function init() {
	}

	//运行服务，系统服务自动运行
	public function run() {
	}
	
	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}
	
	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>